{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e77e7e81",
   "metadata": {},
   "source": [
    "这个代码主要由两部分组成，第一部分是一个正弦波，这部分是copy的；另一部分是重采样，这部分也是copy的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "174cad09",
   "metadata": {},
   "source": [
    "虽然后面的东西没有做，但好歹这是我第一个仅凭百度和微信就完成的代码，也算是我python道路上的里程碑了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15f65307",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import random\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import interpolate\n",
    "\n",
    "def sin_wave(A, f, fs, phi, t):\n",
    "    '''\n",
    "    :params A:    振幅\n",
    "    :params f:    信号频率\n",
    "    :params fs:   采样频率\n",
    "    :params phi:  相位\n",
    "    :params t:    时间长度\n",
    "    '''\n",
    "    # 若时间序列长度为 t=1s, \n",
    "    # 采样频率 fs=1000 Hz, 则采样时间间隔 Ts=1/fs=0.001s\n",
    "    # 对于时间序列采样点个数为 n=t/Ts=1/0.001=1000, 即有1000个点,每个点间隔为 Ts\n",
    "    Ts = 1/fs\n",
    "    n = t / Ts\n",
    "    n = np.arange(n)\n",
    "    y = A*np.sin(2*np.pi*f*n*Ts + phi*(np.pi/180))\n",
    "    return y\n",
    "# f=50 hz\n",
    "fs = 4800\n",
    "hz_50 = sin_wave(A=1, f=50, fs=fs, phi=0, t=0.08)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bae8ff4",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "flinear = interpolate.interp1d(x, hz_50)\n",
    "fcubic = interpolate.interp1d(x, hz_50, kind='cubic')\n",
    "\n",
    "xnew = np.arange(0.001, 1, 1)\n",
    "ylinear = flinear(xnew)\n",
    "ycubic = fcubic(xnew)\n",
    "plt.plot(x, hz_50, 'X', xnew, ylinear, 'x', xnew, ycubic, 'o')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
